package scr 
{
	import cell.KeyPad;
	import flash.events.Event;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import img.ScalePlane;
	import ru.gosoplata.model.TmpModel;
	import ru.gosoplata.view.FooterView;
	import uicontrols.ButtonBase;
	/**
	 * ...
	 * @author a.orleansky
	 */
	public class ScreenExchangeCode extends ScreenBase implements IScreen
	{
		private var btn_next:ButtonBase;
		private var btn_confirm:ButtonBase;
		private var numpad:KeyPad;
		private var inputfield:ScalePlane;
		private var codestxt:TextField = new TextField();
		private var up_txt:TextField;
		private var down_txt:TextField;
		private var code:String="";
		private var codes:Array = new Array();
		private var codelen:Number = 10;
		private var pic_count:Number=0;
		
		private var _footer:FooterView=new FooterView();
		
		public function ScreenExchangeCode() 
		{
			super("enter_exchange_code");
		}
		
		override protected function init(e:Event):void 
		{
			super.init(e);
			
			addChild(_footer);
			
			btn_next = new ButtonBase("next", 0x00FF00);
			btn_next.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_next.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			addChild(btn_next);
			pic_count++;
			
			btn_confirm = new ButtonBase("confirm_excode", 0x00FF00,TmpModel.IMG_FOLDER+"/sdacha/text_pogoshenie_sdachi.png");
			btn_confirm.addEventListener(ButtonBase.ON_PRESS_UI_BUTTON, pressBtn);
			btn_confirm.addEventListener(ButtonBase.ON_PIC_LOADED, allPicLoaded);
			addChild(btn_confirm);
			pic_count++;
			
			numpad = new KeyPad();
			numpad.num_limit = 10;
			numpad.addEventListener(KeyPad.KEY_PAD_RELEASED, update);
			addChild(numpad);
			
			inputfield = new ScalePlane(700);
			addChild(inputfield);
			
			//loadBanner(TmpModel.IMG_FOLDER+"/sotovie_operatori/images/reklama_title.png");
		}
		
		override public function reDraw():void
		{
			btn_next.visible = false;
			codes = new Array();
			
			_footer.reDraw();
		}		
		public function addInputField():void
		{
			var micro_fmt:TextFormat = TmpModel.getTextFormat(16, 0x676767,true);
			micro_fmt.letterSpacing = 1;
			
			codestxt = new TextField();
			codestxt.embedFonts = true;
			codestxt.y = inputfield.y - 40;
			codestxt.x = inputfield.x;
			codestxt.width = 600;
			codestxt.height = 40;
			codestxt.border = false;
			codestxt.selectable = false;
			codestxt.defaultTextFormat = micro_fmt;
			codestxt.antiAliasType = AntiAliasType.ADVANCED;
			codestxt.thickness = 100;
			addChild(codestxt);
			
			up_txt = new TextField();
			up_txt.embedFonts = true;
			up_txt.y = inputfield.y + inputfield.height-10;
			up_txt.x = codestxt.x; 
			up_txt.width = 700;
			up_txt.height = 60;
			up_txt.defaultTextFormat = micro_fmt;
			up_txt.antiAliasType = AntiAliasType.ADVANCED;
			up_txt.thickness = -50;
			up_txt.multiline = true;
			up_txt.selectable = false;
			up_txt.text = 'Введите "Код для использования" указанный на чеке в поле выше:\nВыберите необходимое действие, нажав кнопку ниже:';
			addChild(up_txt);
			
			down_txt = new TextField();
			down_txt.embedFonts = true;
			down_txt.defaultTextFormat = micro_fmt;
			down_txt.width = 700;
			down_txt.height = 250;
			down_txt.x = up_txt.x;
			down_txt.y = btn_confirm.y+btn_confirm.height+10;
			down_txt.thickness = -50;
			down_txt.selectable = false;
			down_txt.antiAliasType = AntiAliasType.ADVANCED;
			down_txt.text = 'Кнопка "Вперед" станет активной \n' +
			'после внесения всех данных\n' +
			'Если вы хотите ввести несколько кодов для погашения сдачи, \n' +
			'после выбора действия и ввода кода нажмите С и продолжите ввод.\n'+
			'Коды будут отображены через запятую в поле выше.';
			addChild(down_txt);

		}		
		
		private function update(e:Event):void 
		{
			if ((e.target as KeyPad).entered_value.length <= codelen)
			{
				code = (e.target as KeyPad).entered_value;
				inputfield.text = code;
			}
		}
		
		private function pressBtn(e:Event):void 
		{
			switch(e.target.name) 
			{
				case "next":
					TmpModel.tmpGlobalController.onClick("submitexcode",codes);
					break;
				case "confirm_excode":
					if(code.length==codelen)confirmCode();
					break;
			}
		}
		
		private function confirmCode():void 
		{
			codes.push(code);
			code = "";
			inputfield.text = "";
			codestxt.text = codes.join(",");
			btn_next.visible = true;
			numpad.clearEnteredValue();
		}
		
		private function allPicLoaded(e:Event):void 
		{
			pic_count--;
			if (pic_count == 0) 
			{
				
				btn_next.x = stage.stageWidth - (btn_next.width+20);
				btn_next.y = stage.stageHeight - (btn_next.height + 20);
				
				btn_confirm.x = btn_next.width/2+20;
				btn_confirm.y = stage.stageHeight / 2 - btn_confirm.height;
				//btn_confirm.addIcon(TmpModel.IMG_FOLDER+"/sdacha/text_pogoshenie_sdachi.png");
				
				inputfield.x = Math.ceil(btn_confirm.x);
				inputfield.y = 250;

				numpad.x = inputfield.x + inputfield.width + 50;
				numpad.y = inputfield.y;
				addInputField();
			}
		}	
	}
}